<?php
namespace Admin\Controller;
use Admin\AppConst\WebConst;
use Admin\Model\AdminModel;
use Admin\Service\AdminService;
use Admin\Service\RoleService;
use Admin\Service\UserService;

class AccountController extends BaseController
{
    private $listUrl;
    public function __construct()
    {
        parent::__construct();
        $this->actionTitle  = '后台账号';
        $this->assign('actionTitle', $this->actionTitle);

        $this->listUrl  = '/Admin/Account/getList';
        $this->assign('listUrl', $this->listUrl);
    }

    /* @function    login       登录
     * @date        2018-07-28
     * @author      zhangle
     */
    public function login(){

        $email      = $this->getParams['email'];            //邮箱
        $password   = $this->getParams['password'];         //密码

        if(checkEmail($email) == WebConst::DEFAULT_TYPE_FALSE){
            $this->error('邮箱格式错误!');
        }

        if(checkPassword($password) == WebConst::DEFAULT_TYPE_FALSE){
            $this->error('密码格式错误!');
        }

        $result = AdminService::get()->login($email, $password);
        if($result['status'] == WebConst::DEFAULT_TYPE_FALSE){
            $this->error($result['message']);
        }else{
            redirect('/Admin/Index/index');
        }
    }

    /* @function    登录界面
     * @date        2018-08-01
     * @author      zhangle
     */
    public function index(){
        $this->display('login');
    }

    /* @function    退出
     * @date        2018-08-01
     * @author      zhangle
     */
    public function loginOut(){
        session(WebConst::USER_SESSION_KEY, WebConst::DEFAULT_TYPE_NULL);   //用户信息
        session(WebConst::MENU_SESSION_KEY, WebConst::DEFAULT_TYPE_NULL);   //菜单信息
        redirect('index');
    }

    /* @function    createAccount      //创建账户
     * @date        2018-08-01
     * @author      zhangle
     */
    public function getList(){
        $list       = AdminService::get()->getList();
        $roleList   = RoleService::get()->getListTitle();
        foreach ($list as $key => $item) {
            $statusMsg  = '<ins class="btn bg-success btn-sm">正常</ins>';
            if($item['is_delete'] == 1){
                $statusMsg  = '<ins class="btn bg-orange btn-sm">冻结</ins>';
            }
            $list[$key]['statusMsg']    = $statusMsg;
            $list[$key]['roleTitle']    = $roleList[$item['role_id']];

        }
        $this->assign('list', $list);
        $this->display();
    }

    /* @function    createAccount      //创建账户
     * @date        2018-08-01
     * @author      zhangle
     */
    public function createAccount(){
        if(!$_POST){
            $roleList   = RoleService::get()->getList();
            $this->assign('roleList', $roleList);
            $this->display();
            exit;
        }
        //开始创建
        $email          = $this->getParams['email'];            //邮箱
        $password       = $this->getParams['password'];         //密码
        $againPassword  = $this->getParams['againPassword'];    //二次密码
        $name           = $this->getParams['name'];             //姓名
        $roleId         = intval($this->getParams['roleId']);   //权限id

        if(checkEmail($email) == WebConst::DEFAULT_TYPE_FALSE){
            $this->error('邮箱格式错误!');
        }

        if(checkPassword($password) == WebConst::DEFAULT_TYPE_FALSE){
            $this->error('密码格式错误!');
        }

        if($password !== $againPassword){
            $this->error('两次密码输入不一致!');
        }

        $result = AdminService::get()->createAccount($email, $password, $name, $roleId);
        if($result['status'] == WebConst::DEFAULT_TYPE_FALSE){
            $this->error($result['message']);
        }else{
            $this->success('创建成功!', $this->listUrl);
        }

    }


    /* @function    更新
     * @date        2018-08-02
     * @author      zhangle
     */
    public function editAccount(){

        $id = intval($this->getParams['id']);
        if($id < WebConst::CATEGORY_TYPE_DEFAULT){
            $this->error('id参数错误!');
        }

        if(empty($_POST)){
            //获取详情
            $admin      = AdminModel::get()->getAccountByUserId($id);
            $roleList   = RoleService::get()->getList();
            $this->assign('data', $admin);
            $this->assign('roleList', $roleList);
            //获取当前数据

            $this->display();
            exit;
        }

        //开始更新

        $password       = $this->getParams['password'];         //密码
        $againPassword  = $this->getParams['againPassword'];    //二次密码
        $name           = $this->getParams['name'];             //姓名
        $roleId         = intval($this->getParams['roleId']);   //权限id

        $isUpdatePassword   = WebConst::DEFAULT_TYPE_FALSE;
        if(!empty($password) || !empty($againPassword)){
            if(checkPassword($password) == WebConst::DEFAULT_TYPE_FALSE){
                $this->error('密码格式错误!');
            }

            if($password !== $againPassword){
                $this->error('两次密码输入不一致!');
            }

            $isUpdatePassword   = WebConst::DEFAULT_TYPE_TRUE;
        }

        $result = AdminService::get()->editAccount($id, $name, $roleId, $isUpdatePassword, $password);

        if($result == WebConst::DEFAULT_TYPE_TRUE){
            $this->success('更新成功!', $this->listUrl);
        }else{
            $this->error('更新失败!');
        }
    }

    /* @function    删除
     * @date        2018-08-02
     * @author      zhangle
     */
    public function deleteAccount(){

        $id = intval($this->getParams['id']);
        if($id < WebConst::CATEGORY_TYPE_DEFAULT){
            $this->error('id参数错误!');
        }

        $result     =   AdminService::get()->deleteAccount($id);

        if($result == WebConst::DEFAULT_TYPE_TRUE){
            $this->success('冻结成功!', $this->listUrl);
        }else{
            $this->error('冻结失败!');
        }

    }

    /* @function    删除
     * @date        2018-08-02
     * @author      zhangle
     */
    public function deleteAppAccount(){

        $id = intval($this->getParams['id']);
        if($id < WebConst::CATEGORY_TYPE_DEFAULT){
            $this->error('id参数错误!');
        }

        $result     =   UserService::get()->deleteAccount($id);

        if($result == WebConst::DEFAULT_TYPE_TRUE){
            $this->success('冻结成功!', '/Admin/Account/getAppAccountList');
        }else{
            $this->error('冻结失败!');
        }

    }

    /* @function    createAccount      //创建账户
     * @date        2018-08-01
     * @author      zhangle
     */
    public function getAppAccountList(){
        $list   = UserService::get()->getList();
        foreach ($list as $key => $item) {
            $statusMsg  = '<ins class="btn bg-success btn-sm">正常</ins>';
            if($item['is_delete'] == 1){
                $statusMsg  = '<ins class="btn bg-orange btn-sm">冻结</ins>';
            }
            $list[$key]['statusMsg']  = $statusMsg;

        }
        $this->assign('list', $list);
        $this->display();
    }

}
